<template>
	<view class="gooddetails" :style="{'margin-top':statusHeight+'px'}">
		<view class="sticky-box">
			<view class="user-panel">
				<view class="userinfo">
					<view class="username">
						<view class="flex-row" style="align-items: center;">
							<image src="/static/img/return.png" style="width: 42rpx; height: 42rpx;"
								@click="returnList"></image>
							<view><text
									style="font-weight: bold; margin-right: 10rpx; font-size: 32rpx;">{{goodsDetails.PatientName}}</text>
								 | {{goodsDetails.PatientIdCode}}</view>
						</view>
					</view>
					<view class="address">{{goodsDetails.PatientAddress}}</view>
					<view class="phone">{{goodsDetails.Mobile}}</view>
				</view>
			</view>
			<view class="search-panel">
				<search-input ref="searchInput" placeholder="追溯扫码" @searchConfirm="handleSearch" rightText="清空"
					:isRight="true" :isFocus="isFocus">
				</search-input>
			</view>
		</view>
		<view class="box">
			<view class="flex-row box-row" style="margin-top: 0;">
				<view class="left">
					<text>处方单号</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.Code}}</text>
				</view>
			</view>
			<view class="flex-row box-row" style="margin-top: 0;">
				<view class="left">
					<text>处方时间</text>
				</view>
				<view class="right">
					<text>{{goodsDetails.UpdateTime}}</text>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="goods-header">
				已扫药品：
			</view>
			<view class="goods-panel" v-for="(good,gIndex) in goodsDetails.PharmacyStockOutDetailList">
				<view class="goods-info flex-row">
					<view class="info-name"> {{good.Name}}</view></text>
					<view style="color:#5d71cb"> {{good.Price}}</view></text>
				</view>
				<view class="goods-info flex-row">
					<view class=""> {{good.Manufacturer}}</view></text>
					<view class=""> {{good.ApprovalNumber}}</view></text>
				</view>
				<view class="goods-info flex-row">
					<view class=""> {{good.PackageSpec}}</view></text>
				</view>

				<view class="goods-info flex-row">
					<view class="good-amount">
						<view class="good-amount-label">总数量</view>
						<view class="good-amount-num">{{good.Amount}}</view>
					</view>
					<view class="good-scan">
						<view class="good-amount-label">已扫数</view>
						<view class="good-amount-num">{{good.scan}}</view>
					</view>
					<view class="good-noscan">
						<view class="good-amount-label">未扫数</view>
						<view class="good-amount-num">{{good.Amount>good.scan?good.Amount-good.scan:0}}</view>
					</view>
				</view>
			</view>
		</view>


		<view class="flex-row footer-btn">
			<view class="common-btn" @tap="ToHome">
				<text>首页</text>
			</view>
			<view class="yellow-btn" @tap="handleNoAccept">
				<text>拒签</text>
			</view>
			<view class="common-btn" @tap="saveSupervisionCode('last')">
				<text>签收</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import searchInput from '@/components/search-input/search-input.vue'
	var that;
	export default {
		components: {
			searchInput
		},
		data() {
			return {
				searchVal: '',
				codes: '',
				isFocus: true,
				ScanCodeAmount: 0,
				goodsDetails: {},
				statusHeight: 0,
				codeList: [], //待提交入库的商品溯源码列表
				scanGoodsList: [], //已扫商品列表
				currentGood: {} //当前正扫的商品
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				stockInGoods: state => state.goods.stockInGoods, // stockInGoods
				scanCode: state => state.goods.scanCode, // scanCode				
			})
		},
		onLoad() {
			that = this
			that.statusHeight = uni.getSystemInfoSync().statusBarHeight;
		},
		onShow() {
			that.getServerData()
		},
		onUnload() {
			that.cleanScanCode()
		},
		onReady() {
			that.$refs.searchInput.setfocus();
		},
		methods: {
			...mapMutations(['initScanCode', 'cleanScanCode', 'initCompanyGood', 'initCompanyGoods']),
			//返回首页
			returnList() {
				history.go(-1);
			},
			getServerData() {
				that.goodsDetails = uni.getStorageSync('companyGood') || {}
				//console.log(that.goodsDetails)
			},
			//扫描
			handleSearch(res) {
				that.searchVal = res.value;
				that.updateProductNum();
			},

			//清除重扫
			handleCleanSearch() {
				that.$refs.searchInput.cleanInput()
			},

			//清除重扫
			CleanAgainScan() {
				uni.showModal({
					title: '提示',
					confirmText: '继续',
					content: '确认清空重扫吗？',
					success: function(res) {
						if (res.confirm) {
							that.$refs.searchInput.cleanInput()
							that.codeList = [];
							that.getServerData();
						}
					}
				});	
			},

			ToHome() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			//拒收
			handleNoAccept() {
				uni.showModal({
					title: ' 警告',
					confirmText: '确定',
					content: '确定拒收吗',
					success: function(res) {
						if (res.confirm) {
							let requestData = {
								ID: that.goodsDetails.ID
							};
							uni.showLoading({
								title: '提交中'
							})
							that.$request.globalRequest('supplierstock/wpstockoutreject', 'POST', requestData,
								'', 0).then(res => {
								uni.hideLoading()
								that.$Router.back()

							}).catch(res => {
								uni.hideLoading()
								that.$common.showNormalToast(res);
								//console.log(res);
							})
						} else if (res.cancel) {

						}
					}
				});
			},

			//扫码更新商品入库数量  step 1
			updateProductNum() {
				if (that.searchVal === '') {
					that.$common.showNormalToast('未采集到溯源码');
					return
				}
				
				//取溯源码的前七位
				let ssyCode = that.searchVal.substring(0, 7);
				if (that.codeList.indexOf(that.searchVal) != -1) {
					//判断是否是重复扫码
					that.$common.showNormalToast('重复扫码');
					that.$refs.searchInput.cleanInput()
					return;
				} else {
					that.goodsDetails.PharmacyStockOutDetailList.forEach(good => {						
						//查找匹配的商品
						var matchGood = good.GoodsCheckCodeList.filter(item => item.SupervisionCode == ssyCode);
						if (matchGood && matchGood.length > 0) {
							
							if (that.scanGoodsList.length > 0)
							{
								if (good.Id == that.currentGood.Id)
								{			
									if (that.currentGood.scan+matchGood[0].UnitRatio>good.Amount)
									{
										that.$common.showNormalToast('药品扫码数据超出处方数量');
										return;
									}
									that.currentGood.scan += matchGood[0].UnitRatio									
									that.currentGood.scanCodeList.push(that.searchVal)
									that.codeList.push(that.searchVal);
								}
								else{									
									that.saveSupervisionCode('',good,matchGood[0].UnitRatio);									
								}
							}
							else {
								good.scan += matchGood[0].UnitRatio;
								that.currentGood = good;
								that.currentGood.scanCodeList.push(that.searchVal);
								that.scanGoodsList.unshift(that.currentGood);
								that.codeList.push(that.searchVal);
							}							
						}
						
					})
					that.$refs.searchInput.cleanInput();
				}
			},

			//确定入库,分为不同类型，如果有批次的话
			saveSupervisionCode(type,nextGood,unitRatio) {
				let valid = true;
				if (that.scanGoodsList.length == 0)
				{
					that.$common.showNormalToast('请先扫溯源码');
					return;
				}
				
				
				let good = that.currentGood;
				if (good.scan != good.Amount) {
					that.$common.showNormalToast('扫码数量与处方单药品数量不一致，请继续扫码');
					return;
				}
				uni.showLoading({
					title: '提交中'
				})
				let rqData = {
					ID: good.Id,
					CodeList: good.scanCodeList,
					PackageRatio: good.PackageRatio,
					ScanedNum: good.scan
				}
				console.log(rqData)
				that.$request.globalRequest('supplierstock/wpconfirmstockout', 'POST', rqData, '', 0)
					.then(res => {
						//如果是最后一个商品，整个处方单提交
						if (type == "last") {
							this.chufangQianshou();
						}
						else{
							that.currentGood = nextGood;
							that.currentGood.scan += unitRatio
							that.currentGood.scanCodeList.push(that.searchVal)
							that.scanGoodsList.unshift(that.currentGood);
							that.codeList.push(that.searchVal)
						}
						uni.hideLoading()
					}).catch(res => {
						uni.hideLoading()
					})
			},

			//处方签收
			chufangQianshou() {
				let vilid = true;
				that.goodsDetails.PharmacyStockOutDetailList.forEach(good => {
					if (good.scan != good.Amount) {
						vilid = false;
					}
				})

				if (!vilid) {
					that.$common.showNormalToast('扫码数量与处方单药品数量不一致，请继续扫码');
					return;
				}
				
				let requestData = {
					ID: that.goodsDetails.ID
				};
				that.$request.globalRequest('supplierstock/wprecievestockout', 'POST',
						requestData, '', 0)
					.then(res => {
						console.log(res)
						that.$common.showNormalToast('签收成功');
						that.$Router.back();
						uni.hideLoading()
				
					}).catch(res => {
						uni.hideLoading()
						that.$common.showNormalToast(res);
					})
			},

			searchBlur() {
				that.isFocus = false
			},
			searchFocus() {
				// that.isFocus = true
				// uni.hideKeyboard()
			},
			searchInput(e) {
				that.code = e.detail.value
			},
			inputFoucs() {
				// that.isFocus = false
				// setTimeout(() => {
				// 	that.isFocus = true
				// }, 100)
				// uni.hideKeyboard()
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "goodsDetail.scss";
</style>